<template>
  <div class="login">
    <div class="login-container clearfix">
      <div class="go-back" @click="$router.back()">
        <i class="iconfont icon-arrow-left-bold"></i>
      </div>
      <h1 class="title">饱了么</h1>
      <div class="login-type">
        <a @click="loginByText = true" :class="{ on: loginByText }">短信登录</a>
        <a @click="loginByText = false" :class="{ on: !loginByText }">密码登录</a>
      </div>
      <form class="login-form" @submit.prevent="login" v-show="loginByText">
        <input type="tel" placeholder="手机号" v-model="phone" />
        <input type="text" placeholder="验证码" v-model="textCaptcha" />
        <button class="captcha-btn" @click.prevent="">获取验证码</button>
        <p class="remind">
          温馨提示：未注册本平台帐号的手机号，登录时将自动注册，且代表已同意
          <a href="#">《用户服务协议》</a>
        </p>
        <button class="login-btn" @submit.prevent="">登录</button>
        <a href="#" class="about-us">关于我们</a>
      </form>

      <form class="login-form" @submit.prevent="login" v-show="!loginByText">
        <input type="text" placeholder="手机 / 邮箱 / 用户名" v-model="userName" />
        <div class="password-part">
          <input type="password" placeholder="密码" v-if="!showPwd" v-model="pwd" />
          <input type="text" placeholder="密码" v-else v-model="pwd" />
          <div
            class="slide-btn"
            :class="{ showPwd: showPwd }"
            @click="showPwd = !showPwd"
          >
            <div class="bar"></div>
            <span class="slider"></span>
          </div>
        </div>
        <div class="captcha-part">
          <input type="text" placeholder="验证码" v-model="imgCaptcha" />
          <!-- <img
            src="http://localhost:4001/captcha"
            alt=""
            @click="getImgCaptcha"
            ref="ImgCaptcha"
          /> -->
        </div>
        <button class="login-btn" @submit.prevent="">登录</button>
        <a href="#" class="about-us">关于我们</a>
      </form>
    </div>

    <AlertTip v-show="showAlert" :alertMsg="alertMsg" @closeTip="closeTip"></AlertTip>
  </div>
</template>

<script>
import AlertTip from "../../components/AlertTip/AlertTip"
export default {
  name: "Login",
  data() {
    return {
      //是否短信登录  控制显示哪个表单
      loginByText: false,
      //是否显示密码
      showPwd: false,
      //用户手机号
      phone: "",
      //短信验证码
      textCaptcha: "",
      //用户手机/邮箱/用户名
      userName: "",
      //用户密码
      pwd: "",
      //图形验证码
      imgCaptcha: "",
      //alert提示信息
      alertMsg: "",
      //是否显示alert
      showAlert: false,
    }
  },
  methods: {
    login() {
      //如果是短信登录
      if (this.loginByText) {
        //用正则表达式验证手机号格式,略
        if (!this.phone) {
          this.showAlertFn("请输入正确的手机号")
          return
        }
        //用正则验证验证码是否是6位数,略
        else if (!this.textCaptcha) {
          this.showAlertFn("您输入的验证码有误")
          return
        }

        //发送短信验证码的登录请求
      }
      //如果是用户名和密码登录
      else {
        const { userName, pwd, imgCaptcha } = this
        if (!userName) {
          this.showAlertFn("请输入用户名")
          return
        } else if (!pwd) {
          this.showAlertFn("请输入密码")
          return
        } else if (!imgCaptcha) {
          this.showAlertFn("请输入验证码")
          return
        }

        //发送用户名和密码的登录请求
      }
    },
    showAlertFn(alertMsg) {
      this.alertMsg = alertMsg
      this.showAlert = true
    },
    closeTip() {
      this.showAlert = false
      this.alertMsg = ""
    },
    //重新获取验证码
    // getImgCaptcha() {
    //   /*
    //   改变<img>的src属性, 就会重新发送请求.
    //   这里把一个时间戳作为参数连在url后面, 它的作用就是让浏览器认为这是一个新的请求地址, 从而不读取之前的缓存
    //   */
    //   this.$refs.ImgCaptcha.src = "http://localhost:4001/captcha?time=" + Date.now()
    // },
  },
  components: { AlertTip },
}
</script>

<style lang="less" scoped>
@import "../../common/style/mixin.less";
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  .go-back {
    position: absolute;
    top: 1.3rem;
    left: 1rem;
    .iconfont {
      font-size: 2.5rem;
      color: #999;
    }
  }
  .title {
    margin: 6rem 0 0 0;
    font-size: 4rem;
    line-height: 1;
    text-align: center;
    font-weight: 700;
    color: @theme-color;
  }

  .login-type {
    margin: 4rem 0 0 0;
    text-align: center;
    a {
      color: #333;
      font-size: 1.4rem;
      margin: 0 2.2rem;
      padding-bottom: 0.4rem;
      &.on {
        color: @theme-color;
        font-weight: 700;
        border-bottom: 0.2rem solid @theme-color;
      }
    }
  }

  .login-form {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    input {
      margin-top: 1.6rem;
      padding-left: 1rem;
      width: 30rem;
      height: 4.8rem;
      border: 0.1rem solid #ddd;
      border-radius: 0.4rem;
      outline: none;
      font-size: 1.4rem;
      &:focus {
        border: 0.1rem solid @theme-color;
      }
    }
    .captcha-btn {
      position: absolute;
      top: 3.2rem;
      right: 4.7rem;
      border: 0;
      color: #ccc;
      font-size: 1.4rem;
      background: transparent;
    }
    .remind {
      margin: 1.2rem 0 0 0;
      width: 30rem;
      height: 4rem;
      color: #999;
      font-size: 1.4rem;
      line-height: 2rem;
      a {
        color: @theme-color;
      }
    }
    .login-btn {
      width: 30rem;
      height: 4.2rem;
      margin-top: 3rem;
      border-radius: 0.4rem;
      background: darken(@theme-color, -10%);
      color: #fff;
      font-size: 1.6rem;
      line-height: 4.2rem;
      border: none;
    }
    .about-us {
      font-size: 1.2rem;
      margin-top: 2rem;
      text-align: center;
      color: #999;
    }
    .password-part {
      position: relative;
      .slide-btn {
        position: absolute;
        top: 48%;
        right: 1.5rem;
        width: 4rem;
        height: 2rem;
        border: 0.1rem solid #dddddd;
        border-radius: 1rem;
        transition: 0.3s;
        .slider {
          display: inline-block;
          width: 1.8rem;
          height: 1.8rem;
          border-radius: 50%;
          background-color: #fff;
          border: 0.1rem solid #ddd;
          box-shadow: 0 0.2rem 0.4rem rgba(0, 0, 0, 0.1);
          transition: 0.3s;
        }
        &.showPwd {
          background-color: @theme-color;
          .slider {
            transform: translateX(2rem);
          }
        }
      }
    }
    .captcha-part {
      position: relative;
      img {
        position: absolute;
        top: 28%;
        right: 0;
        width: 13rem;
      }
    }
  }
}
</style>
